﻿using BLL;
using DAL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;

namespace _11._4WebApI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoomTypeController : ControllerBase
    {
        public RoomTypeController (RoomTypeBLL roomTypeBLL)
        {
            RoomTypeBLL = roomTypeBLL;
        }
        public RoomTypeBLL RoomTypeBLL { get; }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="roomType"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public int Add(RoomType roomType)
        {
            try
            {

                return RoomTypeBLL.Add(roomType);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        [HttpDelete]
        public int Delete(int id)
        {
            try
            {

                return RoomTypeBLL.Delete(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="roomType"></param>
        /// <returns></returns>
        /// 
        [HttpPut]
        public int Update(RoomType roomType)
        {
            try
            {

                return RoomTypeBLL.Update(roomType);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpGet]
        public List<RoomType> GetShow()
        {
            try
            {
                return RoomTypeBLL.GetShow();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 反添
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 

        [HttpGet]
        public RoomType GetType(int id)
        {
            try
            {
                return RoomTypeBLL.GetType(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="name"></param>
        /// <param name="Count"></param>
        /// <param name="Sum"></param>
        /// <param name="Index"></param>
        /// <param name="Size"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public IActionResult Show(string? name, int Index = 1, int Size = 2)
        {
            try
            {
                int Count = 0;
                int Sum = 0;

                var list = RoomTypeBLL.Show(name, out Count, out Sum, Index, Size);
                return Ok(new
                {
                    data =list, 
                    Count = Count,
                    Sum = Sum
                });
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpPost]
        public IActionResult Pic(IFormFile file)
        {
            string newFile = Directory.GetCurrentDirectory() + @"\wwwroot\Pic\" + file.FileName;
            using (FileStream fs = new FileStream(newFile, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            //输出文件路径
            return Ok("http://localhost:5256/Pic/" + file.FileName);
        }
    }
}
